package com.sz.pc.service.impl.excel;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.sz.pc.entity.model.ResultData;
import com.sz.pc.entity.model.ResultDataEnum;
import com.sz.pc.entity.model.excel.InvestExcelModel;
import com.sz.pc.service.interfs.excel.IInvestExcelService;
import com.sz.pc.utils.excel.ExcelUtil;

@Service
public class InvestExcelServiceImpl implements IInvestExcelService
{
	private List<String> columnsList = null;
	private final Logger logger = LoggerFactory.getLogger(InvestExcelServiceImpl.class);
	@Override
	public ResultData importInvestExcel(MultipartFile file) 
	{
		try 
		{
			initColumnList();
			List<InvestExcelModel> excelVO = excelToVO(file,columnsList);
			//1查询投资人
			validExcelData(excelVO);
			//批量插入
		} catch (Exception e) {
			return new ResultData(ResultDataEnum.SERVER_ERROR.getCode(),e.getMessage(),null);
		}
		return new ResultData("");
	}
	
	/**
	 * 表格数据转换为实体
	 * @param file
	 * @param columnsList
	 * @return
	 */
	public List<InvestExcelModel> excelToVO(MultipartFile file,List<String> columnsList) 
	{
		List<InvestExcelModel> list = new ArrayList<InvestExcelModel>();
		try 
		{
			Workbook wb = ExcelUtil.getWorkbook(file);//根据Excel数据源创建WorkBook
			Sheet sheet=wb.getSheetAt(0);
	
			for(int i=4;i<=sheet.getLastRowNum();i++)
			{
				//行的第一个单元格没有数据则认为改行数据
				Row row = sheet.getRow(i);
				if (row == null) 
				{
					continue;
				}
                Cell firstCell=row.getCell(0);
                String firstContent = ExcelUtil.getStringVal(firstCell);
                if(firstCell==null || "".equals(firstContent))
                {
                	continue;
                }
                
                InvestExcelModel vo = new InvestExcelModel();
            	for (int j=0 ;j<columnsList.size();j++) 
            	{
            		//获取当前单元格中的内容
            		String content=ExcelUtil.getStringVal(row.getCell(j));
            		ExcelUtil.setFieldValueByName(columnsList.get(j), content, vo);
//            		vo.setPhone(ExcelUtil.getStringVal(row.getCell(1)));
//            		vo.setName(ExcelUtil.getStringVal(row.getCell(2)));
//            		vo.setPhone(ExcelUtil.getStringVal(row.getCell(3)));
//            		vo.setPhone(ExcelUtil.getStringVal(row.getCell(4)));
                	list.add(vo);
            	}
                 
			}
		} catch (Exception e) {
			logger.error("导入Excel失败",e);
			throw new RuntimeException("导入Excel失败");
		}
        return list;
	}
	/**
	 * 初始化列
	 */
	private void initColumnList(){
		columnsList = new ArrayList<String>();
		columnsList.add("no");
		columnsList.add("realName");
		columnsList.add("idcard");
		columnsList.add("mobile");
		columnsList.add("provinceName");
		columnsList.add("cityName");
		columnsList.add("countyName");
		columnsList.add("townName");
		columnsList.add("cun");
		columnsList.add("village");
		columnsList.add("tudiArea");
		columnsList.add("familyIncome");
		columnsList.add("population");
		columnsList.add("labouringPopulation");
		columnsList.add("repaymentAbility");
		columnsList.add("familyCredit");
		columnsList.add("cunCredit");
		columnsList.add("zhenCrdit");
		columnsList.add("xianCredit");
		columnsList.add("consoleCredit");
		columnsList.add("danbaoName");
		columnsList.add("danbaoRef");
		columnsList.add("needLoan");
		columnsList.add("product");
		columnsList.add("payType");
	}
	
	public void validExcelData(List<InvestExcelModel> vo)
	{
		
	}

}
